<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="数据结构,算法,链表,">





  <link rel="alternate" href="/blog/atom.xml" title="编程那点事" type="application/atom+xml">






<meta name="description" content="学习JavaScript数据结构与算法（一）：栈与队列学习JavaScript数据结构与算法（二）：链表学习JavaScript数据结构与算法（三）：集合学习JavaScript数据结构与算法（四）：二叉搜索树  链表简介链表是一种常见的数据结构，也属于线性表，但不会按线性的顺序来储存数据。而是在每一个节点中，储存了下一个节点的指针。可以看图理解。(有C语言基础的可能比较好理解)。使用链表结构可">
<meta name="keywords" content="数据结构,算法,链表">
<meta property="og:type" content="article">
<meta property="og:title" content="学习JavaScript数据结构与算法（二）：链表">
<meta property="og:url" content="https://lhweb.gitee.io/blog/2018/09/23/学习JavaScript数据结构与算法（二）：链表/index.html">
<meta property="og:site_name" content="编程那点事">
<meta property="og:description" content="学习JavaScript数据结构与算法（一）：栈与队列学习JavaScript数据结构与算法（二）：链表学习JavaScript数据结构与算法（三）：集合学习JavaScript数据结构与算法（四）：二叉搜索树  链表简介链表是一种常见的数据结构，也属于线性表，但不会按线性的顺序来储存数据。而是在每一个节点中，储存了下一个节点的指针。可以看图理解。(有C语言基础的可能比较好理解)。使用链表结构可">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://lhweb.gitee.io/blog/2018/09/23/学习JavaScript数据结构与算法（二）：链表/link1.png">
<meta property="og:image" content="https://lhweb.gitee.io/blog/2018/09/23/学习JavaScript数据结构与算法（二）：链表/dlink.png">
<meta property="og:updated_time" content="2019-10-03T05:53:45.949Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="学习JavaScript数据结构与算法（二）：链表">
<meta name="twitter:description" content="学习JavaScript数据结构与算法（一）：栈与队列学习JavaScript数据结构与算法（二）：链表学习JavaScript数据结构与算法（三）：集合学习JavaScript数据结构与算法（四）：二叉搜索树  链表简介链表是一种常见的数据结构，也属于线性表，但不会按线性的顺序来储存数据。而是在每一个节点中，储存了下一个节点的指针。可以看图理解。(有C语言基础的可能比较好理解)。使用链表结构可">
<meta name="twitter:image" content="https://lhweb.gitee.io/blog/2018/09/23/学习JavaScript数据结构与算法（二）：链表/link1.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://lhweb.gitee.io/blog/2018/09/23/学习JavaScript数据结构与算法（二）：链表/">





  <title>学习JavaScript数据结构与算法（二）：链表 | 编程那点事</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">编程那点事</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://lhweb.gitee.io/blog/blog/2018/09/23/学习JavaScript数据结构与算法（二）：链表/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="lhweb">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="编程那点事">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">学习JavaScript数据结构与算法（二）：链表</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-09-23T17:38:52+08:00">
                2018-09-23
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/javascript/" itemprop="url" rel="index">
                    <span itemprop="name">javascript</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p><a href="https://lhweb.gitee.io/blog/2018/09/23/%E5%AD%A6%E4%B9%A0JavaScript%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95%EF%BC%88%E4%B8%80%EF%BC%89%EF%BC%9A%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97/">学习JavaScript数据结构与算法（一）：栈与队列</a><br><a href="https://lhweb.gitee.io/blog/2018/09/23/%E5%AD%A6%E4%B9%A0JavaScript%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95%EF%BC%88%E4%BA%8C%EF%BC%89%EF%BC%9A%E9%93%BE%E8%A1%A8/">学习JavaScript数据结构与算法（二）：链表</a><br><a href="https://lhweb.gitee.io/blog/2018/09/23/%E5%AD%A6%E4%B9%A0JavaScript%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95%EF%BC%88%E4%B8%89%EF%BC%89%EF%BC%9A%E9%9B%86%E5%90%88/">学习JavaScript数据结构与算法（三）：集合</a><br><a href="https://lhweb.gitee.io/blog/2018/09/23/%E5%AD%A6%E4%B9%A0JavaScript%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95%EF%BC%88%E5%9B%9B%EF%BC%89%EF%BC%9A%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91/">学习JavaScript数据结构与算法（四）：二叉搜索树</a></p>
</blockquote>
<h1 id="链表简介"><a href="#链表简介" class="headerlink" title="链表简介"></a>链表简介</h1><p>链表是一种常见的数据结构，也属于线性表，但不会按线性的顺序来储存数据。而是在每一个节点中，储存了下一个节点的指针。可以看图理解。(有C语言基础的可能比较好理解)。<br>使用链表结构可以克服数组需要预先知道数据大小的缺点(C语言的数组需要预先定义长度)，链表结构可以充分利用计算机内存空间，实现灵活的内存动态管理。</p>
<p>接下来就是介绍两种常见的链表: 单向链表，双向链表在JavaScript中的实现。</p>
<h1 id="单向链表"><a href="#单向链表" class="headerlink" title="单向链表"></a>单向链表</h1><p>链表中最简单的形式就是单向链表，链表中的节点都包含两个部分，第一部分储存着自身信息，第二部分则储存有指向下一节点的指针。最后一个节点则指向NULL，如图所示:<br><img src="/blog/2018/09/23/学习JavaScript数据结构与算法（二）：链表/link1.png" alt="单向链表图示2"></p>
<h2 id="JavaScipt中单向链表的实现"><a href="#JavaScipt中单向链表的实现" class="headerlink" title="JavaScipt中单向链表的实现"></a>JavaScipt中单向链表的实现</h2><p>首先，创建一个构造函数。<br><figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 单向链表构造函数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">LinkedList</span><span class="params">()</span> </span>&#123;</span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 单向链表中节点的构造函数</span></span><br><span class="line"><span class="comment">   * @param &#123;Any&#125; element 要传入链表的节点</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="keyword">var</span> Node = <span class="function"><span class="keyword">function</span><span class="params">(element)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.element = element;</span><br><span class="line">    <span class="comment">//下个节点的地址</span></span><br><span class="line">    <span class="keyword">this</span>.next = <span class="literal">null</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">//单向链表的长度</span></span><br><span class="line">  <span class="keyword">var</span> length = <span class="number">0</span>;</span><br><span class="line">  <span class="comment">//单向链表的头结点，初始化为NULL</span></span><br><span class="line">  <span class="keyword">var</span> head = <span class="literal">null</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>不难看出，单向链表构造函数比栈与队列要复杂许多。</p>
<p>单向链表需要有如下的方法:</p>
<ul>
<li><p>append(element): 添加元素到链表尾部</p>
</li>
<li><p>insert(position,element): 向单向链表中某个位置插入元素</p>
</li>
<li><p>indexOf(element): 寻找某个元素在单向链表中的位置</p>
</li>
<li><p>remove(element): 移除给定的元素</p>
</li>
<li><p>removeAt(position): 移除单向链表中某个位置的元素</p>
</li>
<li><p>getHead(): 获取单向链表的头部</p>
</li>
<li><p>isAmpty(): 检查单向链表是否为空，为空则返回true</p>
</li>
<li><p>toString(): 将链表所有内容以字符串输出</p>
</li>
<li><p>size(): 返回单向链表长度</p>
</li>
</ul>
<h3 id="append方法"><a href="#append方法" class="headerlink" title="append方法:"></a>append方法:</h3><p>说明: 向单向链表尾部添加元素。<br>实现:<br><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 向单向链表尾部添加元素</span><br><span class="line"> * @param  &#123;Any&#125; element 要加入链表的节点</span><br><span class="line"> */</span><br><span class="line">this.append = function(element) &#123;</span><br><span class="line">  var <span class="keyword">node</span> <span class="title">= new</span> <span class="keyword">Node</span><span class="title">(element</span>);</span><br><span class="line">  var current;</span><br><span class="line"></span><br><span class="line">  if (head == null) &#123;</span><br><span class="line">    head = <span class="keyword">node</span><span class="title">;</span></span><br><span class="line"><span class="title">  &#125; else</span> &#123;</span><br><span class="line">    // 当前项等于链表头部元素.</span><br><span class="line">    // while循环到最后一个，从而将该节点加入链表尾部。</span><br><span class="line">    current = head;</span><br><span class="line">    // 当next为null时，判定为<span class="literal">false</span>。退出循环。</span><br><span class="line">    while (current.next) &#123;</span><br><span class="line">      current = current.next;</span><br><span class="line">    &#125;</span><br><span class="line">    current.next = <span class="keyword">node</span><span class="title">;</span></span><br><span class="line"><span class="title">  &#125;</span></span><br><span class="line"><span class="title">  length</span>++;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="insert方法"><a href="#insert方法" class="headerlink" title="insert方法:"></a>insert方法:</h3><p>说明: 向单向链表中某个位置插入元素。<br>实现:<br><figure class="highlight xquery"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 向单向链表中插入某个元素</span><br><span class="line"> * @param  &#123;Number&#125;<span class="built_in"> position</span> 要插入的位置</span><br><span class="line"> * @param  &#123;Any&#125; <span class="keyword">element</span>  要插入的元素</span><br><span class="line"> * @return &#123;Boolean&#125;          插入成功返<span class="built_in">回true</span>，失败返<span class="built_in">回false</span></span><br><span class="line"> */</span><br><span class="line">this.<span class="keyword">insert</span> = <span class="keyword">function</span><span class="built_in">(position</span>, <span class="keyword">element</span>) &#123;</span><br><span class="line">  <span class="keyword">if</span> <span class="built_in">(position</span> &gt;= <span class="number">0</span> &amp;&amp;<span class="built_in"> position</span> &lt;= length) &#123;</span><br><span class="line">    var <span class="type">node</span> = new Node(<span class="keyword">element</span>);</span><br><span class="line">    var current = head;</span><br><span class="line">    var previous;</span><br><span class="line">    var index = 0;</span><br><span class="line"></span><br><span class="line">    if (position == 0) &#123;</span><br><span class="line">      <span class="type">node</span>.<span class="keyword">next</span> = current;</span><br><span class="line">     <span class="built_in"> head</span> = <span class="type">node</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      while (index++ &lt;<span class="built_in"> position</span>) &#123;</span><br><span class="line">        <span class="keyword">previous</span> = current;</span><br><span class="line">        current = current.<span class="keyword">next</span>;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">previous</span>.<span class="keyword">next</span> = <span class="type">node</span>;</span><br><span class="line">      <span class="type">node</span>.<span class="keyword">next</span> = current;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    length++;</span><br><span class="line">    <span class="keyword">return</span><span class="built_in"> true</span>;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">return</span><span class="built_in"> false</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="indexOf方法"><a href="#indexOf方法" class="headerlink" title="indexOf方法:"></a>indexOf方法:</h3><p>说明：寻找某个元素在单向链表中的位置。<br>实现:<br><figure class="highlight xquery"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 寻找某个元素在单向链表中的位置</span><br><span class="line"> * @param  &#123;Any&#125; <span class="keyword">element</span> 要寻找的元素</span><br><span class="line"> * @return &#123;Number&#125;         返回值&gt;=<span class="number">0</span>则代表找到相应位置</span><br><span class="line"> */</span><br><span class="line">this.indexOf = <span class="keyword">function</span>(<span class="keyword">element</span>) &#123;</span><br><span class="line">  var current =<span class="built_in"> head</span>;</span><br><span class="line">  var index = -<span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">  while (current) &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">element</span> === current.<span class="keyword">element</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> index;</span><br><span class="line">    &#125;</span><br><span class="line">    index++;</span><br><span class="line">    current = current.<span class="keyword">next</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="remove方法"><a href="#remove方法" class="headerlink" title="remove方法:"></a>remove方法:</h3><p>说明: 移除给定的元素。<br>实现:<br><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 移除给定的元素</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span>  &#123;Any&#125; element 要移除的元素</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> &#123;Number&#125;         返回值&gt;=0表示移除成功</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">this</span>.remove = function(element) &#123;</span><br><span class="line">  <span class="keyword">var</span> index = <span class="keyword">this</span>.indexOf(element);</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>.removeAt(index);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="removeAt方法"><a href="#removeAt方法" class="headerlink" title="removeAt方法:"></a>removeAt方法:</h3><p>说明:移除单向链表中某个位置的元素。<br>实现:<br><figure class="highlight xquery"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 移除单向链表中某一个元素</span><br><span class="line"> * @param  &#123;Number&#125;<span class="built_in"> position</span> 要移除元素的位置</span><br><span class="line"> * @<span class="keyword">return</span> &#123;Any&#125;          移除成功返回被移除的元素，不成功则返回NULL</span><br><span class="line"> */</span><br><span class="line">this.removeAt = <span class="keyword">function</span><span class="built_in">(position</span>) &#123;</span><br><span class="line">  <span class="keyword">if</span> <span class="built_in">(position</span> &gt; -<span class="number">1</span> &amp;&amp;<span class="built_in"> position</span> &lt; length) &#123;</span><br><span class="line">    var current =<span class="built_in"> head</span>;</span><br><span class="line">    var <span class="keyword">previous</span>;</span><br><span class="line">    var index = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">(position</span> == <span class="number">0</span>) &#123;</span><br><span class="line">      // 因为之<span class="built_in">前head</span>指向第一个元素，现在<span class="built_in">把head</span>修改为指向第二个元素。</span><br><span class="line">      // 核心概念在于链表前后全靠指针链接，而非数组一般。</span><br><span class="line">      // 所以只需要改<span class="built_in">变head</span>的元素。</span><br><span class="line">     <span class="built_in"> head</span> = current.<span class="keyword">next</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      while (index++ &lt;<span class="built_in"> position</span>) &#123;</span><br><span class="line">        // <span class="keyword">previous</span>指要操作元素位置之前的那个元素，current表示之后的那个元素。</span><br><span class="line">        <span class="keyword">previous</span> = current;</span><br><span class="line">        current = current.<span class="keyword">next</span>;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">previous</span>.<span class="keyword">next</span> = current.<span class="keyword">next</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    length--;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> current.<span class="type">element</span>;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> null;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="getHead方法"><a href="#getHead方法" class="headerlink" title="getHead方法:"></a>getHead方法:</h3><p>说明:获取单向链表的头部。<br>实现:<br><figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取单向链表的头部</span></span><br><span class="line"><span class="comment"> * @return &#123;Any&#125; 单向链表的头部</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">this</span>.getHead = <span class="function"><span class="keyword">function</span><span class="params">()</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> head;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="isAmpty、toString、size方法"><a href="#isAmpty、toString、size方法" class="headerlink" title="isAmpty、toString、size方法"></a>isAmpty、toString、size方法</h3><p>实现:<br><figure class="highlight qml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 判断单向链表是否为空</span></span><br><span class="line"><span class="comment"> * @return &#123;Boolean&#125; 为空则返回true，不为空则返回false</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">this</span>.isAmpty = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> length === <span class="number">0</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 将链表所有内容以字符串输出</span></span><br><span class="line"><span class="comment"> * @return &#123;String&#125; 要输出的字符串</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">this</span>.toString = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> current = head;</span><br><span class="line">  <span class="keyword">var</span> <span class="built_in">string</span> = <span class="string">''</span>;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">while</span> (current) &#123;</span><br><span class="line">    <span class="built_in">string</span> += current.element;</span><br><span class="line">    current = current.next;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> <span class="built_in">string</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 返回单向链表长度</span></span><br><span class="line"><span class="comment"> * @return &#123;Number&#125; 单向链表的长度</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">this</span>.size = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> length;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h1 id="双向链表"><a href="#双向链表" class="headerlink" title="双向链表"></a>双向链表</h1><p>双向链表与单向链表很是相像。在单向链表中，只有指向下一个节点的链接。但在双向链表中，还有指向上一个节点的链接，是双向的。<br>如图所示:<br><img src="/blog/2018/09/23/学习JavaScript数据结构与算法（二）：链表/dlink.png" alt="双向链表图示"></p>
<h2 id="JavaScipt中双向链表的实现"><a href="#JavaScipt中双向链表的实现" class="headerlink" title="JavaScipt中双向链表的实现"></a>JavaScipt中双向链表的实现</h2><p>首先，依然是构造函数:<br><figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 双向链表的构造函数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">DoublyLinkedList</span><span class="params">()</span> </span>&#123;</span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 双向链表中节点的构造函数</span></span><br><span class="line"><span class="comment">   * @param &#123;Any&#125; element 要传入链表的元素</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="keyword">var</span> Node = <span class="function"><span class="keyword">function</span><span class="params">(element)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.element = element;</span><br><span class="line">    <span class="keyword">this</span>.prev = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">this</span>.next = <span class="literal">null</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">//双向链表的长度</span></span><br><span class="line">  <span class="keyword">var</span> length = <span class="number">0</span>;</span><br><span class="line">  <span class="comment">//双向链表的头结点，初始化为NULL</span></span><br><span class="line">  <span class="keyword">var</span> head = <span class="literal">null</span>;</span><br><span class="line">  <span class="comment">//双向链表的尾结点，初始化为NULL</span></span><br><span class="line">  <span class="keyword">var</span> tail = <span class="literal">null</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>双向链表需要有如下的方法:</p>
<ul>
<li><p>append(element): 添加元素到双向链表尾部</p>
</li>
<li><p>insert(position,element): 向双向链表中某个位置插入元素</p>
</li>
<li><p>removeAt(position): 移除双向链表中某个位置的元素</p>
</li>
<li><p>showHead(): 获取双向链表的头部</p>
</li>
<li><p>showLength(): 获取双向链表长度</p>
</li>
<li><p>showTail(): 获取双向链表尾部</p>
</li>
</ul>
<h3 id="append方法-1"><a href="#append方法-1" class="headerlink" title="append方法:"></a>append方法:</h3><p>说明: 添加元素到双向链表尾部<br>实现:<br><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 向链表尾部添加元素</span><br><span class="line"> * @param  &#123;Any&#125; element 要加入链表的节点</span><br><span class="line"> * @return &#123;Any&#125;         加入链表的节点</span><br><span class="line"> */</span><br><span class="line">this.append = function(element) &#123;</span><br><span class="line">  var <span class="keyword">node</span> <span class="title">= new</span> <span class="keyword">Node</span><span class="title">(element</span>);</span><br><span class="line"></span><br><span class="line">  if (head === null) &#123;</span><br><span class="line">    head = <span class="keyword">node</span><span class="title">;</span></span><br><span class="line"><span class="title">    tail</span> = <span class="keyword">node</span><span class="title">;</span></span><br><span class="line"><span class="title">  &#125; else</span> &#123;</span><br><span class="line">    var previous;</span><br><span class="line">    var current = head;</span><br><span class="line"></span><br><span class="line">    while (current.next) &#123;</span><br><span class="line">      current = current.next;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    current.next = <span class="keyword">node</span><span class="title">;</span></span><br><span class="line"><span class="title">    node</span>.prev = current;</span><br><span class="line">    tail = <span class="keyword">node</span><span class="title">;</span></span><br><span class="line"><span class="title">  &#125;</span></span><br><span class="line"><span class="title"></span></span><br><span class="line"><span class="title">  length</span>++;</span><br><span class="line">  return <span class="keyword">node</span><span class="title">;</span></span><br><span class="line"><span class="title">&#125;;</span></span><br></pre></td></tr></table></figure></p>
<h3 id="insert方法-1"><a href="#insert方法-1" class="headerlink" title="insert方法:"></a>insert方法:</h3><p>说明: 向双向链表中某个位置插入元素。<br>实现:<br><figure class="highlight xquery"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 向链表中插入某个元素</span><br><span class="line"> * @param  &#123;Number&#125;<span class="built_in"> position</span> 要插入的位置</span><br><span class="line"> * @<span class="keyword">return</span> &#123;Boolean&#125;         插入成功返<span class="built_in">回true</span>，失败返<span class="built_in">回false</span></span><br><span class="line"> */</span><br><span class="line">this.<span class="keyword">insert</span> = <span class="keyword">function</span><span class="built_in">(position</span>, <span class="keyword">element</span>) &#123;</span><br><span class="line">  <span class="keyword">if</span> <span class="built_in">(position</span> &gt;= <span class="number">0</span> &amp;&amp;<span class="built_in"> position</span> &lt;= length) &#123;</span><br><span class="line"></span><br><span class="line">    var <span class="type">node</span> = new Node(<span class="keyword">element</span>);</span><br><span class="line">    var index = 0;</span><br><span class="line">    var previous;</span><br><span class="line">    var current = head;</span><br><span class="line"></span><br><span class="line">    if (position === 0) &#123;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">if</span> <span class="built_in">(head</span> === null) &#123;</span><br><span class="line">       <span class="built_in"> head</span> = <span class="type">node</span>;</span><br><span class="line">       <span class="built_in"> tail</span> = <span class="type">node</span>;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        current.prev = <span class="type">node</span>;</span><br><span class="line">        <span class="type">node</span>.<span class="keyword">next</span> = current;</span><br><span class="line">       <span class="built_in"> head</span> = <span class="type">node</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> <span class="built_in">(position</span> === length) &#123;</span><br><span class="line"></span><br><span class="line">      current =<span class="built_in"> tail</span>;</span><br><span class="line">      current.<span class="keyword">next</span> = <span class="type">node</span>;</span><br><span class="line">      <span class="type">node</span>.prev = current;</span><br><span class="line">     <span class="built_in"> tail</span> = <span class="type">node</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line"></span><br><span class="line">      while (index++ &lt;<span class="built_in"> position</span>) &#123;</span><br><span class="line">        <span class="keyword">previous</span> = current;</span><br><span class="line">        current = current.<span class="keyword">next</span>;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">previous</span>.<span class="keyword">next</span> = <span class="type">node</span>;</span><br><span class="line">      <span class="type">node</span>.prev = <span class="keyword">previous</span>;</span><br><span class="line">      current.prev = <span class="type">node</span>;</span><br><span class="line">      <span class="type">node</span>.<span class="keyword">next</span> = current;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    length++;</span><br><span class="line">    <span class="keyword">return</span><span class="built_in"> true</span>;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">return</span><span class="built_in"> false</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="removeAt方法-1"><a href="#removeAt方法-1" class="headerlink" title="removeAt方法:"></a>removeAt方法:</h3><p>说明:移除双向链表中某个位置的元素。<br>实现:<br><figure class="highlight xquery"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 移除链表中某一个元素</span><br><span class="line"> * @param  &#123;Number&#125;<span class="built_in"> position</span> 要移除元素的位置</span><br><span class="line"> * @<span class="keyword">return</span> &#123;Any&#125;             移除成功返回被移除的元素，不成功则返<span class="built_in">回false</span></span><br><span class="line"> */</span><br><span class="line">this.removeAt = <span class="keyword">function</span><span class="built_in">(position</span>) &#123;</span><br><span class="line">  <span class="keyword">if</span> <span class="built_in">(position</span> &gt; -<span class="number">1</span> &amp;&amp;<span class="built_in"> position</span> &lt; length) &#123;</span><br><span class="line">    var current =<span class="built_in"> head</span>;</span><br><span class="line">    var index = <span class="number">0</span>;</span><br><span class="line">    var <span class="keyword">previous</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">(position</span> === <span class="number">0</span>) &#123;</span><br><span class="line">     <span class="built_in"> head</span> = current.<span class="keyword">next</span>;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">if</span> (length === <span class="number">1</span>) &#123;</span><br><span class="line">       <span class="built_in"> tail</span> = null;</span><br><span class="line">       <span class="built_in"> head</span>.prev = null;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> <span class="built_in">(position</span> === length - <span class="number">1</span>) &#123;</span><br><span class="line">      current =<span class="built_in"> tail</span>;</span><br><span class="line">     <span class="built_in"> tail</span> = current.prev;</span><br><span class="line">     <span class="built_in"> tail</span>.<span class="keyword">next</span> = null;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      while (index++ &lt;<span class="built_in"> position</span>) &#123;</span><br><span class="line">        <span class="keyword">previous</span> = current.prev;</span><br><span class="line">        current = current.<span class="keyword">next</span>;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">previous</span>.<span class="keyword">next</span> = current.<span class="keyword">next</span>;</span><br><span class="line">      current.<span class="keyword">next</span>.prev = <span class="keyword">previous</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    length--;</span><br><span class="line">    <span class="keyword">return</span> current.<span class="type">element</span>;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">return</span><span class="built_in"> false</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="showHead、showLength、showTail方法"><a href="#showHead、showLength、showTail方法" class="headerlink" title="showHead、showLength、showTail方法"></a>showHead、showLength、showTail方法</h3><p>实现:<br><figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取链表的头部</span></span><br><span class="line"><span class="comment"> * @return &#123;Any&#125; 链表的头部</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">this</span>.showHead = <span class="function"><span class="keyword">function</span><span class="params">()</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> head;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取链表长度</span></span><br><span class="line"><span class="comment"> * @return &#123;Number&#125; 链表长度</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">this</span>.showLength = <span class="function"><span class="keyword">function</span><span class="params">()</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> length;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取链表尾部</span></span><br><span class="line"><span class="comment"> * @return &#123;Any&#125; 链表尾部</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">this</span>.showTail = <span class="function"><span class="keyword">function</span><span class="params">()</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> tail;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<p>感想<br>链表这一节，基本全部都是先按需求写代码，写完后再和书上对比。发现简直被瞬间秒成渣。自己写的很多暗坑，逻辑也很混乱。看来还是太年轻了。</p>
<p>有兴趣的同学，也可以自己试试只看要求先写代码，写完后再与书上比对，就知道自己的不足了。</p>
<p>前端路漫漫，且行且歌~</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/数据结构/" rel="tag"># 数据结构</a>
          
            <a href="/blog/tags/算法/" rel="tag"># 算法</a>
          
            <a href="/blog/tags/链表/" rel="tag"># 链表</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/09/23/javascript之数组操作/" rel="next" title="javascript之数组操作">
                <i class="fa fa-chevron-left"></i> javascript之数组操作
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/09/23/学习JavaScript数据结构与算法（三）：集合/" rel="prev" title="学习JavaScript数据结构与算法（三）：集合">
                学习JavaScript数据结构与算法（三）：集合 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">lhweb</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">39</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">25</span>
                  <span class="site-state-item-name">tags</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/blog/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#链表简介"><span class="nav-number">1.</span> <span class="nav-text">链表简介</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#单向链表"><span class="nav-number">2.</span> <span class="nav-text">单向链表</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#JavaScipt中单向链表的实现"><span class="nav-number">2.1.</span> <span class="nav-text">JavaScipt中单向链表的实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#append方法"><span class="nav-number">2.1.1.</span> <span class="nav-text">append方法:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#insert方法"><span class="nav-number">2.1.2.</span> <span class="nav-text">insert方法:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#indexOf方法"><span class="nav-number">2.1.3.</span> <span class="nav-text">indexOf方法:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#remove方法"><span class="nav-number">2.1.4.</span> <span class="nav-text">remove方法:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#removeAt方法"><span class="nav-number">2.1.5.</span> <span class="nav-text">removeAt方法:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#getHead方法"><span class="nav-number">2.1.6.</span> <span class="nav-text">getHead方法:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#isAmpty、toString、size方法"><span class="nav-number">2.1.7.</span> <span class="nav-text">isAmpty、toString、size方法</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#双向链表"><span class="nav-number">3.</span> <span class="nav-text">双向链表</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#JavaScipt中双向链表的实现"><span class="nav-number">3.1.</span> <span class="nav-text">JavaScipt中双向链表的实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#append方法-1"><span class="nav-number">3.1.1.</span> <span class="nav-text">append方法:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#insert方法-1"><span class="nav-number">3.1.2.</span> <span class="nav-text">insert方法:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#removeAt方法-1"><span class="nav-number">3.1.3.</span> <span class="nav-text">removeAt方法:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#showHead、showLength、showTail方法"><span class="nav-number">3.1.4.</span> <span class="nav-text">showHead、showLength、showTail方法</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">lhweb</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
